//1.1导入fs模块
const fs = require('fs')
//1.2导入path模块
const path = require('path')
//1.3导入http模块
const http = require('http')

//2.1创建web服务器
const server = http.createServer()
//2.2监听web服务器的request事件
server.on('request', (req, res) => {
  //3.1 获取到客户端请求的url地址
  const url = req.url 
  //3.2将请求的url地址映射为具体文件的存放路径
  // const fpath = path.join(__dirname, url)
  //5.1预定义空白的文件存放路径
  let fpath = ''
  if (url === '/') {
    fpath = path.join(__dirname, './clock/index.html')
  } else {
    fpath = path.join(__dirname, '/clock', url)
  }

  //4.1读取文件
  fs.readFile(fpath, 'utf8', (err, data) => {
    if (err) {
      return res.end('404 Not Found')      
    }
    res.end(data)
  })
})
//2.3启动web服务器
server.listen(80, () => {
  console.log('server running at http://127.0.0.1')
  
})